// HookInet.cpp : implementation file
//

#include "stdafx.h"
#include "dlltool.h"
#include "DlgHookInet.h"
#include "HOOKInet.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CHookInet dialog


CDlgHookInet::CDlgHookInet(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgHookInet::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgHookInet)
	m_strCreatefileaaddress = _T("");
	m_filename = _T("");
	m_GetProcAddressAddr = _T("");
	m_funname = _T("");
	m_strDllName = _T("");
	//}}AFX_DATA_INIT
}


void CDlgHookInet::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgHookInet)
	DDX_Text(pDX, IDC_EDIT_CreateFileAADDR, m_strCreatefileaaddress);
	DDV_MaxChars(pDX, m_strCreatefileaaddress, 8);
	DDX_Text(pDX, IDC_EDIT_ADDRFILENAMEA, m_filename);
	DDX_Text(pDX, IDC_EDIT_GetProcAddressADDR2, m_GetProcAddressAddr);
	DDX_Text(pDX, IDC_EDIT_FAR_FUNNAME, m_funname);
	DDX_Text(pDX, IDC_EDIT_DLLNAME1, m_strDllName);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgHookInet, CDialog)
	//{{AFX_MSG_MAP(CDlgHookInet)
	ON_BN_CLICKED(IDC_BN_INTERNETOPEN, OnBnInternetopen)
	ON_BN_CLICKED(IDC_BN_HTTPOPENREUEST, OnBnHttpopenreuest)
	ON_BN_CLICKED(IDC_BN_HttpSendRequest, OnBNHttpSendRequest)
	ON_BN_CLICKED(IDC_BN_InternetWriteFile, OnBNInternetWriteFile)
	ON_BN_CLICKED(IDC_BN_CreateFileA, OnBNCallCreateFileA)
	ON_BN_CLICKED(IDC_BN_GetProcAddress, OnBNGetProcAddress)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHookInet message handlers

void CDlgHookInet::OnBnInternetopen() 
{
	// TODO: Add your control notification handler code here
//	char abc[512];
//	::GetModuleFileName(NULL,abc,512);
	int nret = HookInternetOpen();
	switch(nret)
	{
	case 1:
		::SetWindowText(::GetDlgItem(this->m_hWnd,IDC_BN_INTERNETOPEN),"UnInternetopen"); 
		break;
	case 2:
		::SetWindowText(::GetDlgItem(this->m_hWnd,IDC_BN_INTERNETOPEN),"Internetopen"); 
	    break;
	default:
	    break;
	}
}

void CDlgHookInet::OnBnHttpopenreuest() 
{
	// TODO: Add your control notification handler code here
	
	int nret = HookHttpOpenRequest();
	switch(nret)
	{
	case 1:
		::SetWindowText(::GetDlgItem(this->m_hWnd,IDC_BN_HTTPOPENREUEST),"UnHttpOpenReuest"); 
		break;
	case 2:
		::SetWindowText(::GetDlgItem(this->m_hWnd,IDC_BN_HTTPOPENREUEST),"HttpOpenReuest"); 
		break;
	default:
		break;
	}
}

int HookHttpSendRequest();
int HookInternetWriteFile();

void CDlgHookInet::OnBNHttpSendRequest() 
{
	// TODO: Add your control notification handler code here
	
	int nret = HookHttpSendRequest();
	switch(nret)
	{
	case 1:
		::SetWindowText(::GetDlgItem(this->m_hWnd,IDC_BN_HttpSendRequest),"UnHttpSendRequest"); 
		break;
	case 2:
		::SetWindowText(::GetDlgItem(this->m_hWnd,IDC_BN_HttpSendRequest),"HttpSendRequest"); 
		break;
	default:
		break;
	}
}

void CDlgHookInet::OnBNInternetWriteFile() 
{
	// TODO: Add your control notification handler code here
	
	int nret = HookInternetWriteFile();
	switch(nret)
	{
	case 1:
		::SetWindowText(::GetDlgItem(this->m_hWnd,IDC_BN_InternetWriteFile),"UnInternetWriteFile"); 
		break;
	case 2:
		::SetWindowText(::GetDlgItem(this->m_hWnd,IDC_BN_InternetWriteFile),"InternetWriteFile"); 
		break;
	default:
		break;
	}
}

typedef HANDLE (WINAPI
 * PTRCreateFileA)(
    LPCSTR lpFileName,
    DWORD dwDesiredAccess,
    DWORD dwShareMode,
    LPSECURITY_ATTRIBUTES lpSecurityAttributes,
    DWORD dwCreationDisposition,
    DWORD dwFlagsAndAttributes,
    HANDLE hTemplateFile
    );
void CDlgHookInet::OnBNCallCreateFileA() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	m_strCreatefileaaddress.GetBuffer(0);
	unsigned int data = 0;
	if (m_strCreatefileaaddress.GetLength())
	{
		char * paddress =  m_strCreatefileaaddress.GetBuffer(0);
		sscanf(paddress,"%08x",&data);
		PTRCreateFileA fun = (PTRCreateFileA)data;
		HANDLE hFile = fun(m_filename.GetBuffer(0),    // file to open
                   GENERIC_READ,          // open for reading
                   FILE_SHARE_READ,       // share for reading
                   NULL,                  // default security
                   OPEN_EXISTING,         // existing file only
                   FILE_ATTRIBUTE_NORMAL, // normal file
                   NULL);                 // no attr. template 
		if (hFile != INVALID_HANDLE_VALUE) 
		{ 
			CloseHandle(hFile);
		}
	}
	UpdateData(FALSE);
}
typedef FARPROC (WINAPI *
PTRGetProcAddress)(
    HMODULE hModule,
    LPCSTR lpProcName
    );
void CDlgHookInet::OnBNGetProcAddress() 
{
	// TODO: Add your control notification handler code here
	
	UpdateData();
	m_strCreatefileaaddress.GetBuffer(0);
	unsigned int data = 0;
	if (m_strCreatefileaaddress.GetLength())
	{
		char * paddress =  m_GetProcAddressAddr.GetBuffer(0);
		sscanf(paddress,"%08x",&data);
		PTRGetProcAddress fun = (PTRGetProcAddress)data;
		if(HMODULE hdll = LoadLibraryA(m_strDllName.GetBuffer(0)))
		{
			HANDLE hFile = fun(hdll,m_funname.GetBuffer(0));                 // no attr. template 
		}

	}
	UpdateData(FALSE);
}
